package com.derive.education_service.web.wechat.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.derive.education_service.common.enums.WechatData;
import com.derive.education_service.entity.User;
import com.derive.education_service.mapper.UserMapper;
import com.derive.education_service.utils.OkHttpCli;
import com.derive.education_service.web.wechat.service.WxApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信_小程序_登录校验实现接口
 * @author XYD
 */
@Service
@Slf4j
public class WxApiServiceImpl implements WxApiService {

    @Autowired
    private OkHttpCli okHttpCli;

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据code获取openId
     * @param code
     * @return
     */
    @Override
    public Map<String, String> getWxOpenId(String code) {
        // 配置请求参数
        Map<String,String> param = new HashMap<>();
        param.put("appid", WechatData.APP_ID);
        param.put("secret", WechatData.APP_SECRET);
        param.put("js_code", code);
        param.put("grant_type", WechatData.WX_LOGIN_GRANT_TYPE);

        // 向微信第三方服务发送请求
        String wxResult = okHttpCli.doGet(WechatData.WX_LOGIN_URL, param);
        JSONObject jsonObject = JSONObject.parseObject(wxResult);
        log.info("微信服务返回信息 === " + jsonObject);
        Map<String,String> map = new HashMap<>();
        if (jsonObject.containsKey("openid")) {
            // 存在openId 返回openId
            String session_key = jsonObject.get("session_key").toString();
            String open_id = jsonObject.get("openid").toString();
            map.put("openId",open_id);
            map.put("sessionKey",session_key);
        } else {
            // 返回微信提供的错误信息
            map.put("error",jsonObject.get("errmsg").toString());
        }
        return map;
    }

    /**
     * 获取accessToken
     * @return
     */
    @Override
    public Map<String, String> getAccessToken() {
        // 配置请求参数
        Map<String,String> param = new HashMap<>();
        param.put("appid", WechatData.APP_ID);
        param.put("secret", WechatData.APP_SECRET);
        param.put("grant_type", WechatData.WX_ACCESS_TOKEN_TYPE);

        // 向微信第三方服务发送请求
        String wxResult = okHttpCli.doGet(WechatData.WX_TOKEN_URL, param);

        JSONObject jsonObject = JSONObject.parseObject(wxResult);

        log.info("微信服务返回信息 === " + jsonObject);
        Map<String,String> map = new HashMap<>();
        if (jsonObject.containsKey("access_token")) {
            // 存在access_token 返回access_token
            String access_token = jsonObject.get("access_token").toString();
            map.put("access_token",access_token);
        } else {
            // 返回微信提供的错误信息
            map.put("error",jsonObject.get("errmsg").toString());
        }
        return map;
    }

    /**
     * 检查微信小程序用户是否已经存在
     * 注册使用前都会先判断是否已有该用户的存在
     * 已存在则返回用户基本信息，直接登录
     * 不存在则返回错误信息
     * @param openId
     * @return
     */
    @Override
    public User checkUser(String openId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wx_open_id", openId);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }
}
